/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "AppHookModel.h"
#include "WaitE32ImageSize.h"
#include "ReadyForE32Image.h"
#include "ReceiveE32ImageHeader.h"
#include "Sandbox.h"
#include "Log.h"

CAppHookModel::CAppHookModel(MAppHookModelObserver& aObserver) :
    iObserver(aObserver),
    iHostLink(NULL),
    iSandbox(NULL),
    iCurrentState(NULL),
    iStateId(ENotSandboxing),
    iSandboxingError(KErrNone)
    {}

CAppHookModel::~CAppHookModel()
    {
    LOG(_L("-->CAppHookModel::~CAppHookModel"));
    Disconnect();
    LOG(_L("<--CAppHookModel::~CAppHookModel"));
    }

void CAppHookModel::ConnectL()
    {
    LOG(_L("-->CAppHookModel::ConnectL"));
    __ASSERT_ALWAYS(!Connected(), User::Leave(KErrNotReady));
    iHostLink = CHostLink::NewL(*this);
    LOG(_L("<--CAppHookModel::ConnectL"));
    }

void CAppHookModel::Disconnect()
    {
    LOG(_L("-->CAppHookModel::Disconnect"));
    if (Connected())
        {
        delete iSandbox;
        iSandbox = NULL;
        delete iCurrentState;
        iCurrentState = NULL;
        delete iHostLink;
        iHostLink = NULL;
        }
    LOG(_L("<--CAppHookModel::Disconnect"));
    }

TBool CAppHookModel::Connected() const
    {
    return NULL != iHostLink;
    }

CHostLink& CAppHookModel::HostLink()
    {
    //TODO: panic if disconnected
    return *iHostLink;
    }

const CHostLink& CAppHookModel::HostLink() const
    {
    //TODO: panic if disconnected
    return *iHostLink;
    }

void CAppHookModel::CreateSandboxL(TUint32 aImageSize)
    {
    LOG2(_L("-->CAppHookModel::CreateSandboxL(%d)"), aImageSize);
    __ASSERT_ALWAYS(NULL == iSandbox, User::Leave(KErrAlreadyExists));
    iSandbox = CSandbox::NewL(aImageSize);
    LOG(_L("<--CAppHookModel::CreateSandboxL"));
    }

CSandbox& CAppHookModel::Sandbox()
    {
    //TODO: panic if iSandbox is NULL
    return *iSandbox;
    }

const CSandbox& CAppHookModel::Sandbox() const
    {
    //TODO: panic if iSandbox is NULL
    return *iSandbox;
    }

void CAppHookModel::WaitE32ImageSizeComplete(TInt aError)
    {
    LOG2(_L("-->CAppHookModel::WaitE32ImageSizeComplete(%d"), aError);
    if (KErrNone == aError)
        {
        delete iCurrentState;
        iCurrentState = NULL;
        TRAP(aError, iCurrentState = CReadyForE32Image::NewL(*this));
        }
    if (KErrNone != aError)
        {
        CompleteSandboxing(aError);
        }
    else
        {
        iStateId = EReadyForE32Image;
        }
    LOG(_L("<--CAppHookModel::WaitE32ImageSizeComplete"));
    }

void CAppHookModel::ReadyForE32ImageComplete(TInt aError)
    {
    LOG2(_L("CAppHookModel::ReadyForE32ImageComplete(%d"), aError);
    if (KErrNone == aError)
        {
        delete iCurrentState;
        iCurrentState = NULL;
        iStateId = EReceiveE32ImageHeader;
        TRAP(aError, iCurrentState = CReceiveE32ImageHeader::NewL(*this));
        }
    if (KErrNone != aError)
        {
        iObserver.SandboxingComplete(MAppHookModelObserver::ERespondHostSignal, aError);
        Disconnect();
        }
    LOG(_L("<--CAppHookModel::ReadyForE32ImageComplete"));
    }

void CAppHookModel::ReceiveE32ImageHeaderComplete(TInt aError)
    {
    LOG2(_L("CAppHookModel::ReceiveE32ImageHeaderComplete(%d"), aError);
    if (KErrNone == aError)
        {
        delete iCurrentState;
        iCurrentState = NULL;
        //TRAP(aError, iCurrentState = CReceiveImage::NewL(*this));
        }
    if (KErrNone != aError)
        {
        iObserver.SandboxingComplete(MAppHookModelObserver::EReceiveE32ImageHeader, aError);
        Disconnect();
        }
    }

void CAppHookModel::ReceiveImageComplete(TInt aError)
    {
    LOG2(_L("CAppHookModel::ReceiveImageComplete(%d"), aError);
    if (KErrNone == aError)
        {
        delete iCurrentState;
        iCurrentState = NULL;
        //TRAP(aError, iCurrentState = CReceiveMapFileData::NewL(*this));
        }
    if (KErrNone != aError)
        {
        iObserver.SandboxingComplete(MAppHookModelObserver::EReceiveImage, aError);
        Disconnect();
        }
    }

void CAppHookModel::ReceiveMapFileDataComplete(TInt aError)
    {
    LOG2(_L("CAppHookModel::ReceiveMapFileDataComplete(%d"), aError);
    if (KErrNone == aError)
        {
        delete iCurrentState;
        iCurrentState = NULL;
        //TRAP(aError, iCurrentState = CSandboxServer::NewL(*this));
        }
    if (KErrNone != aError)
        {
        iObserver.SandboxingComplete(MAppHookModelObserver::EReceiveMapFileData, aError);
        Disconnect();
        }
    }

void CAppHookModel::SandboxServerComplete(TInt aError)
    {
    LOG2(_L("CAppHookModel::SandboxServerComplete(%d"), aError);
    iObserver.SandboxingComplete(MAppHookModelObserver::ERunSandboxServer, aError);
    Disconnect();
    }

void CAppHookModel::LinkStatusChanged()
	{
	LOG2(_L("-->CAppHookModel::LinkStatusChanged(%d)"), iHostLink->LinkStatus());
	if (CHostLink::ELinkReady == iHostLink->LinkStatus())
		{
		iStateId = EWaitE32ImageSize;
		iCurrentState = CWaitE32ImageSize::NewL(*this);
		}
	else
		{
		CompleteSandboxing(KErrDisconnected);
		}
	LOG(_L("<--CAppHookModel::LinkStatusChanged"));
	}

void CAppHookModel::HostError()
	{
	LOG(_L("-->CAppHookModel::HostError"));
	if (iCurrentState)
	    {
	    delete iCurrentState;
	    iCurrentState = NULL;
	    iSandboxingError = KErrGeneral;
	    DeviceErrorComplete(KErrNone);
	    }
	LOG(_L("<--CAppHookModel::HostError"));
	}

void CAppHookModel::DeviceErrorComplete(TInt aError)
	{
	LOG2(_L("-->CAppHookModel::DeviceErrorComplete(%d)"), aError);
	if (KErrNone != aError)
		{
		iObserver.SandboxingComplete(iStateId, aError);
		iStateId = ENotSandboxing;
		Disconnect();
		}
	else
	    {
	    iObserver.SandboxingComplete(iStateId, iSandboxingError);
	    iSandboxingError = KErrNone;
	    iStateId = ENotSandboxing;
	    LinkStatusChanged();
	    }
	LOG(_L("<--CAppHookModel::DeviceErrorComplete"));
	}

void CAppHookModel::CompleteSandboxing(TInt aError)
	{
	LOG2(_L("-->CAppHookModel::CompleteSandboxing(%d)"), aError);
	delete iCurrentState;
	iCurrentState = NULL;
	iSandboxingError = aError;
	TRAPD(err, iHostLink->DeviceErrorL());
	if (KErrNone != err)
	    {
	    DeviceErrorComplete(err);
	    }
	LOG(_L("<--CAppHookModel::CompleteSandboxing"));
	}

